/*
 * TreeUtility.java
 *
 * Created on July 28, 2007, 8:02 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.afaker.rss.swing.tree;

import java.util.Enumeration;
import javax.swing.JTree;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import com.afaker.rss.gui.tree.RSSTreeNode;

/**
 *
 * @author bruce
 */
public class TreeUtility {
    private JTree tree;
    /** Creates a new instance of TreeUtility */
    public TreeUtility(JTree tree) {
        this.tree=tree;
    }
    /**
     * Expanding or Collapsing All Nodes in a JTree Component
     * If expand is true, expands all nodes in the tree.
     * Otherwise, collapses all nodes in the tree.
     */
    public void expandAll(TreeNode root,boolean expand) {
        
        // Traverse tree from root
        expandAll(new TreePath(root), expand);
    }
    private void expandAll(TreePath parent, boolean expand) {
        // Traverse children
        TreeNode node = (TreeNode)parent.getLastPathComponent();
        if (node.getChildCount() >= 0) {
            for (Enumeration e=node.children(); e.hasMoreElements(); ) {
                RSSTreeNode n = (RSSTreeNode)e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                expandAll(path, expand);
            }
        }        
        // Expansion or collapse must be done bottom-up
        if (expand) {
            tree.expandPath(parent);
        } else {
            tree.collapsePath(parent);
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }
    
}
